/**
 * Project Name:moma_crm
 * File Name:Dict.java
 * package Name:cn.yun.system.dict.model
 * Date:2016年2月26日上午11:03:00
 * Copyright (c) 2016, www.yunjiaowu.cn All Rights Reserved.
 *     /*
   * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
   *
   * Copyright 2017 摩码创想, support@momathink.com
    *
   * This file is part of Jiaowu_v1.0.
   * Jiaowu_v1.0 is free software: you can redistribute it and/or modify
   * it under the terms of the GNU Lesser General Public License as published by
   * the Free Software Foundation, either version 3 of the License, or
   * (at your option) any later version.
   *
   * Jiaowu_v1.0 is distributed in the hope that it will be useful,
   * but WITHOUT ANY WARRANTY; without even the implied warranty of
   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   * GNU Lesser General Public License for more details.
   *
   * You should have received a copy of the GNU Lesser General Public License
   * along with Jiaowu_v1.0.  If not, see <http://www.gnu.org/licenses/>.
   *
   * 这个文件是Jiaowu_v1.0的一部分。
   * 您可以单独使用或分发这个文件，但请不要移除这个头部声明信息.
    * Jiaowu_v1.0是一个自由软件，您可以自由分发、修改其中的源代码或者重新发布它，
   * 新的任何修改后的重新发布版必须同样在遵守LGPL3或更后续的版本协议下发布.
   * 关于LGPL协议的细则请参考COPYING文件，
   * 您可以在Jiaowu_v1.0的相关目录中获得LGPL协议的副本，
   * 如果没有找到，请连接到 http://www.gnu.org/licenses/ 查看。
   *
   * - Author:摩码创想
   * - Contact: support@momathink.com
   * - License: GNU Lesser General Public License (GPL)
   */



package com.momathink.sys.dict.model;

import java.util.List;

import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.ehcache.CacheKit;
import com.momathink.common.annotation.model.Table;
import com.momathink.common.base.BaseModel;
import com.momathink.common.constants.DictKeys;
import com.momathink.common.thread.ThreadParamInit;

/**
 * ClassName:Dict <br/>
 * 
 * 
 * Date:     2016年2月26日 上午11:03:00 <br/>
 * @author   Richie
 * @version  
 * @since    JDK 1.7
 * @see 	 
 */
@SuppressWarnings("serial")
@Table(tableName="pt_dict")
public class Dict extends BaseModel<Dict> {

	
	public static final Dict dao = new Dict();

	/**通过Key 获取相应的值**/
	public String queryValGetDictname(String key){
		Dict dict = cacheGet(key);
		return dict != null?dict.getStr("val"):"";
	}
	
	/**
	 * queryTableNodeRoot:(根字典目录). <br/>
	 * @author Richie
	 * @return
	 * @since JDK 1.7
	 */
	public List<Dict> queryTableNodeRoot() {
		String sql = "select * from pt_dict where parentid is null order by id asc ";
		return dao.find(sql);
	}

	/**
	 * queryChildList:(根据父级id获取子级所有 字典集合). <br/>
	 * @author Richie
	 * @param pId
	 * @return
	 * @since JDK 1.7
	 */
	public List<Dict> queryChildList(String pId) {
		String sql = "select * from pt_dict where parentid = ? order by id asc ";
		return dao.find(sql,pId);
	}

	/***
	 * 根据编码查询
	 * @param numbers
	 * @return
	 */
	public Dict queryByNumbers(String numbers){
		return dao.findFirst("SELECT * FROM pt_dict WHERE numbers = ?", numbers);
	}

	/**
	 * queryAllDictList:(所有的字典项). <br/>
	 * @author Richie
	 * @return
	 * @since JDK 1.7
	 */
	public List<Dict> queryAllDictList() {
		String sql = "select * from pt_dict where state = 1 ";
		List<Dict> list = Dict.dao.find(sql);
		return list;
	}
	
	public Dict queryDictDetail(String id){
		String sql = "select dict.*,pd.dictname parentname from pt_dict dict left join pt_dict pd on pd.id = dict.parentid where dict.id = ? ";
		return dao.findFirst(sql, id);
	}
	
	public List<Dict> queryDictDetailByNum(String num){
		String sql = "select pd.*,dict.dictname parentname from pt_dict dict right join pt_dict pd on dict.id = pd.parentid where dict.numbers = ? ";
		return dao.find(sql, num);
	}

	/**
	 * cacheAdd:(). <br/>
	 * @author Richie
	 * @param dict
	 * @since JDK 1.7
	 */
	public void cacheAdd(Integer id){
		Dict dict = Dict.dao.findById(id);
		cacheAdd(dict);
	}
	public void cacheAdd(Dict dict) {
		
		CacheKit.put(DictKeys.cache_name_system, ThreadParamInit.cacheStart_dict + dict.getStr("numbers"), dict);
		
		List<Dict> childList = queryChildList(dict.getPrimaryKeyValue().toString());
		CacheKit.put(DictKeys.cache_name_system, ThreadParamInit.cacheStart_dict_child + dict.getStr("numbers"), childList);
		
	}
	
	public void cacheRemove(Integer id){
		Dict dict = Dict.dao.findById(id);
		CacheKit.remove(DictKeys.cache_name_system, ThreadParamInit.cacheStart_dict + dict.getStr("numbers"));
		CacheKit.remove(DictKeys.cache_name_system, ThreadParamInit.cacheStart_dict_child + dict.getStr("numbers"));
		
		Integer parentid = dict.getInt("parentid");
		if(null!=parentid){
			Dict parent = Dict.dao.findById(parentid);
			if(null!=parent){
				List<Dict> parentChildren = queryChildList(parentid.toString());
				CacheKit.put(DictKeys.cache_name_system, ThreadParamInit.cacheStart_dict_child + parent.getStr("numbers"), parentChildren );
			}
		}
	}
	
	public Dict cacheGet(String key){
		Dict dict = CacheKit.get(DictKeys.cache_name_system, ThreadParamInit.cacheStart_dict + key);
		if(null == dict){
			dict = queryByNumbers(key);
			if(null != dict)
				cacheAdd(dict);
		}
		return dict;
	}
	
	public List<Dict> cacheGetChild(String key){
		
		List<Dict> list = CacheKit.get(DictKeys.cache_name_system, ThreadParamInit.cacheStart_dict_child + key);
		if(null == list){
			Dict dict = cacheGet(key);
			if(null!=dict)
				list = CacheKit.get(DictKeys.cache_name_system, ThreadParamInit.cacheStart_dict_child + key);
		}
		return list;
	}

	public void updateParentFalse(String oldParentId) {
		String sql = "update pt_dict set isparent = 'false' where id = ? ";
		Db.update(sql, oldParentId);
	}
	
	
	/**
	 * 获取缓存_值
	 * @param numbers
	 * @return
	 */
	public Double cacheGetValToDouble(String numbers){
		Dict dict = cacheGet(numbers);
		return dict == null ? null : Double.valueOf(dict.getStr("val"));
	}
	

}

